package com.es.hr.shop;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.es.business.area.AreaBiz;
import com.es.business.city.CityBiz;
import com.es.business.province.ProvinceBiz;
import com.es.main.LoginUserBeen;
import com.es.util.page.PageBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class ShopSearchDoAction extends ActionSupport
implements ServletRequestAware{
	 HttpServletRequest request;

	public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
		// TODO Auto-generated method stub
		this.request = paramHttpServletRequest;
	}
	
	public String execute() throws Exception{
		String result = "";
		ShopBiz shopBiz = new ShopBiz();
		
		String area_code = this.request.getParameter("emp_area_code");
		if(area_code == null) area_code = "";
		
		String province_code = this.request.getParameter("emp_province_code");
		if(province_code == null) province_code = "";
		
		String city_code = this.request.getParameter("emp_city_code");
		if(city_code == null) city_code = "";
		
		String queryShopCode = this.request.getParameter("queryShopCode");
		String queryShopName = this.request.getParameter("queryShopName");
		
		String curpage = this.request.getParameter("curpage");
		if(curpage == null) curpage = "1";
		int curpag = Integer.parseInt(curpage);
		
	    LoginUserBeen userBean = (LoginUserBeen)request.getSession(false).getAttribute("loginuser");
		
	    String role  = userBean.getLoginRole();	
		//当前角色能访问的权限组
	   String rtmAreaCode = ""; 
		if("zongbu1shiyebu".indexOf(role)>=0){
			rtmAreaCode="";//代表可以看见全部
		}else if("RTM".equals(role)){
			rtmAreaCode = userBean.getEara_code();				
		}else {//CTM&&其他
			rtmAreaCode = userBean.getEara_code();
		}				
	    
		PageBean res = shopBiz.searchShop(area_code, province_code, city_code,queryShopCode,queryShopName, curpag,rtmAreaCode);
		this.request.setAttribute("pageBean", res);
		if("zongbu1shiyebu".indexOf(role)>=0){
			this.request.setAttribute("areaList", new AreaBiz().getAreaList());
		}else{
			this.request.setAttribute("areaList", new AreaBiz().getRTMAreaByCode(userBean.getEara_code()));			
		}	
		this.request.setAttribute("emp_area_code", area_code);
		this.request.setAttribute("emp_province_code", province_code);
		this.request.setAttribute("emp_city_code", city_code);
		this.request.setAttribute("queryShopCode", queryShopCode);
		this.request.setAttribute("queryShopName", queryShopName);
		this.request.setAttribute("emp_role", userBean.getLoginRole());
		if(area_code.length() > 0){
			this.request.setAttribute("provinceList", new ProvinceBiz().getProvinceByAreaCode(area_code));
		}
		if(province_code.length() > 0){
			this.request.setAttribute("cityList", new CityBiz().getCityByProvinceCode(province_code));
		}
		result = "success";
		return result;
	}
	
	public ShopBean getShopBBean(HttpServletRequest request) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException{
		Field[] fds = Class.forName("com.es.hr.shop.ShopBean").getDeclaredFields();   
		ShopBean bean = new ShopBean();
		 
		  for(int i=0;i<fds.length;i++)   
		  {   
			  String d= fds[i].getName();
			  fds[i].set(bean, request.getParameter(d));
		  }
		  return bean;
	}
	 

}
